package com.yfbao.horizon.schedule.tk.log;

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.concurrent.CircuitBreaker;
import org.osgi.service.log.LogEntry;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/logs")
@RequiredArgsConstructor
public class LogCollectorController {
    private final LogDispatcher logDispatcher;
//    private final CircuitBreaker circuitBreaker;

    @PostMapping
    public ResponseEntity<?> collectLogs(@RequestBody List<LogEntry> logs) {
//        if (!circuitBreaker.allowRequest()) {
//            return ResponseEntity.status(429).build();
//        }

        int success = 0;
        for (LogEntry log : logs) {
            if (logDispatcher.addLog(log)) {
                success++;
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("accepted", success);
        map.put("rejected", logs.size() - success);
        return ResponseEntity.ok(map);
    }
}
